package top.dprkshow.practice.queueLinkedList;

/**
 * @author Kent HAN
 * @date 2021/8/10
 */
public class MyQueue {
    MyQueueNode head, tail;

    MyQueue create() {
        MyQueue q = new MyQueue();
        q.head = null;
        q.tail = null;
        return q;
    }

    void enqueue(int data) {
        MyQueueNode newNode = new MyQueueNode();
        newNode.data = data;
        newNode.next = null;
        if (tail != null) {
            tail.next = newNode;
        } else {
            head = newNode;
        }
        tail = newNode;
    }

    int dequeue() {
        if (head == null) {
            throw new RuntimeException("Queue is empty.");
        }
        int value = head.data;
        if (head.next == null) {
            head = null;
            tail = null;
        } else {
            head = head.next;
        }
        return value;
    }

    void printDebug() {
        MyQueueNode tmp = head;
        while (tmp != null) {
            System.out.print(tmp.data + " > ");
            tmp = tmp.next;
        }
        System.out.println();
    }
}
